package com.letoken.platform.job.jobhandler.monitor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.letoken.platform.pub.constant.ConstantsValue;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.util.LocalDateTool;
import com.letoken.platform.repo.app.dao.AppUserWalletAddMapper;
import com.letoken.platform.repo.app.dao.AppUserWalletMapper;
import com.letoken.platform.repo.app.po.AppUserWallet;
import com.letoken.platform.repo.mall.dao.AppMallDayStatisticsMapper;
import com.letoken.platform.repo.mall.dao.AppMallInfoMapper;
import com.letoken.platform.repo.mall.dao.AppMallOrderInfoMapper;
import com.letoken.platform.repo.mall.dao.AppMallStatisticsMapper;
import com.letoken.platform.repo.mall.po.AppMallDayStatistics;
import com.letoken.platform.repo.mall.req.InitOldData;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@Log4j2
// 发版之后等待第一个CAP用户注册之后
public class AppOnceJob {

    @Autowired
    AppUserWalletMapper appUserWalletMapper;
    @Autowired
    AppUserWalletAddMapper appUserWalletAddMapper;

//    @XxlJob("initCap")
//    public void initCap() {
//        log.info("生成CAP币种记录开始");
//        LambdaQueryWrapper<AppUserWallet> frLqw = new LambdaQueryWrapper<>();
//        frLqw.eq(AppUserWallet::getTokenId, LetokenTokenTypeEnum.CAP.getId()).orderByAsc(AppUserWallet::getId).last("limit 1").select(AppUserWallet::getId);
//        AppUserWallet firstRecord = appUserWalletMapper.selectOne(frLqw);
//        Integer firstId = null;
//        if (ObjectUtils.isNotEmpty(firstRecord)){
//            firstId  = firstRecord.getId();
//        }
//
//        if (ObjectUtils.isEmpty(firstId)){
//            log.info("找不到第一个ID");
//            firstId = appUserWalletMapper.walletMaxId();
//        }
//        LocalDateTime now = LocalDateTime.now();
//
//        List<Integer> list = appUserWalletMapper.walletUserIds(firstId);
//        if (CollectionUtils.isEmpty(list)){
//            log.info("获取不到钱包的用户ID");
//            return;
//        }
//        List<List<Integer>> allUserIds = Lists.partition(list,400);
//        allUserIds.stream().forEach(x->{
//            try {
//                List<AppUserWallet> walletList = new ArrayList<>();
//                for (Integer userId : x){
//                    AppUserWallet capWallet = new AppUserWallet();
//                    capWallet.setUserId(userId);
//                    capWallet.setTokenId(LetokenTokenTypeEnum.CAP.getId());
//                    capWallet.setAvailableBalance(BigDecimal.ZERO);
//                    capWallet.setFreezeBalance(BigDecimal.ZERO);
//                    capWallet.setReleasedBalance(BigDecimal.ZERO);
//                    capWallet.setUpdateTime(now);
//                    capWallet.setCreateTime(now);
//                    capWallet.setVersion(ConstantsValue.I_0);
//                    walletList.add(capWallet);
//                }
//                appUserWalletAddMapper.insertBatchSomeColumn(walletList);
//            } catch (Exception e) {
//                log.error("分批插入失败 {}", JSON.toJSONString(x),e);
//            }
//        });
//
//        log.info("生成CAP币种记录结束");
//    }


}
